home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93b.txt
/
000004_icon-group-sender _Fri Apr 2 07:56:52 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1993-06-16
|
3KB
Received: from owl.CS.Arizona.EDU by cheltenham.CS.Arizona.EDU; Wed, 21 Apr 1993 18:47:50 MST
Received: by owl.cs.arizona.edu; Wed, 21 Apr 1993 18:47:48 MST
Date: 2 Apr 93 07:56:52 GMT
From: ftpbox!mothost!binford!mcdchg!tellab5!ruth.wheaton.edu!gmribeir@uunet.uu.net (Glauber)
Organization: The Bossa Nova University
Subject: Is this passable code?
Message-Id: <1993Apr2.075652.11967@wheaton.wheaton.edu>
Sender: icon-group-request@cs.arizona.edu
To: icon-group@cs.arizona.edu
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
My apologies if this is repeated, but this is the 3rd time
that i'm trying to post this. News sometimes behaves weird
here.
I started using Icon recently, and am impressed with the
power of the language. But, since i'm learning on my own and
don't have experience with Icon, i don't know if i'm doing
things right. Recently i wrote a short program to find
anagrams (yeah...) using the unix dictionary. I'd appreciate
any comments/suggestions/flames, etc about my programming
style and the Icon way.
Thank you very much
Glauber.
----------------------------------------------------------------------
# Find all the anagrams of a given word
# In the Unix dictionary
# Tue Mar 30 07:15:53 CST 1993
procedure main(argv)
word := argv[1]
if not ( in := open("/usr/dict/words") )
then stop("Can't open system dictionary! :-( ")
#initialize
l := map( read(in), &ucase, &lcase )
#main loop
every ( w := lexperm( word ) ) do {
while ( l << w ) do
if not ( l := map( read(in), &ucase, &lcase ) )
then return
# at this point, either (l == w) or (l >> w)
if (l == w) then write(l)
}
end
#I think this is it!
# Lexicographical permutation,
# Reingold, Combinatorial Algorithms, page 162
# (this is a generator)
procedure lexperm( w )
# initialize
w := sortword ( map(w, &ucase, &lcase) )
repeat {
suspend (w)
# now permute
every i := (*w - 1) to 1 by (-1) do {
if w[i] << w[i+1] then break
}
every j := *w to 1 by (-1) do {
if w[i] << w[j] then break
}
w[i] :=: w[j]
r := *w
s := i + 1
while r >= s do {
w[r] :=: w[s]
r -:= 1
s +:= 1
}
# end of permutation
if (j = 1) & (i = 1) then fail
}
end
# insertion sort for now...
procedure sortword( w )
size := *w
# external loop, traverses the word
every j := 2 to size do {
hold := w[j]
# internal loop, "sinks" each letter in place
i := j - 1
while i >= 1 do {
if hold >>= w[i] then break
w[i+1] := w[i]
i -:= 1
}
w[i+1] := hold
}
return w
end
----------------------------------------------------------------------
--
Glauber Ribeiro
glauber@david.wheaton.edu
glauber%david.wheaton.edu@tellab5.tellabs.com
constantly risking absurdity